package	com.sdry.web.controller.jyy.unpack;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sdry.model.jyy.unpack.UnpackDetal;
import com.sdry.model.lz.ReceiveMark;
import com.sdry.service.jyy.unpack.UnpackDetalService;
import com.sdry.utils.DateJsonValueProcessor;
import com.sdry.utils.ResponseUtil;
/**
 *
 *@ClassName: UnpackDetalController
 *@Description: 拆箱明细
 *@Author jyy
 *@Date 2019-08-20 15:58:14
 *@version 1.0
 *
 *修改人：吉永艳
 *修改时间：2019-11-07
 *修改内容：321-332，增加仓库id
*/
@Controller
@RequestMapping("/unpackDetal")
public class UnpackDetalController{
	
	Logger logger = Logger.getLogger(UnpackDetalController.class);  
	
	@Resource UnpackDetalService UnpackDetalService;
	
	
	
	ReceiveMark receiveMarkSrc = new ReceiveMark();
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageUnpackDetal (Model model) {
		return "/td/UnpackDetal";
	}
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public UnpackDetal queryById(Long id) {
		UnpackDetal param = UnpackDetalService.queryById(id);
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<UnpackDetal> queryAllByMution(UnpackDetal param) {
		List<UnpackDetal> list = UnpackDetalService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(UnpackDetal param,HttpServletResponse response) throws Exception{
		// 条件分页查询内容
		List<UnpackDetal> list = UnpackDetalService.findPageByMution(param);
		//条件查询总条数
		Integer total = UnpackDetalService.selectCountByMution(param);
		//转为json数据传至页面
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		//创建result
		JSONObject result = new JSONObject();
		//转json数据
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		//添加入result
		result.put("code", 0);
		result.put("data", jsonArray);
		result.put("count", total);
		ResponseUtil.write(response, result);
	}
	
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	*/
	@RequestMapping("/insert")
	public void insert(UnpackDetal param, HttpServletResponse response, HttpSession httpSession) {
		logger.info("精准拆箱计划明细新增--参数：，"+JSONArray.fromObject(param));
		try {
			Long identity = UnpackDetalService.insertNew(param, httpSession);
			response.getWriter().print(identity);
		} catch (IOException e) {
		logger.error("精准拆箱计划明细新增异常");	
			e.printStackTrace();
		}
	}
	/** 
	 * 插入返回id(批量，不生成条码)
	 * @param param 实体条件
	 * @param response
	*/
	@RequestMapping("/insert2")
	public void insert2(UnpackDetal param, HttpServletResponse response) {
		logger.info("批量拆箱计划明细新增--参数：，"+JSONArray.fromObject(param));
		try {
			param.setState(0);
			Long identity = UnpackDetalService.insert(param);
			response.getWriter().print(identity);
		} catch (IOException e) {
		logger.error("批量拆箱计划明细新增异常");	
			e.printStackTrace();
		}
	}
	
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(UnpackDetal param,HttpServletResponse response) throws Exception{
		Integer count = UnpackDetalService.update(param);
		response.getWriter().print(count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response) throws Exception{
		ids=ids.replace("'",""); 
		Integer count = UnpackDetalService.delete(ids);
		response.getWriter().print(count);
	}
	
	//-----------------------------------------------------app------------------------------------------------------
		/**
		 * 给app查询所有未拆箱及未完成的拆箱单
		 * @param response  响应
		 * 
		 * 修改人：吉永艳
		 * 修改时间：2019-11-07
		 * 修改内容：增加仓库id
		 */
		@RequestMapping("/selectAllUnpacking")
		public void selectAllReceive(String warehouse_id, HttpServletResponse response) {
			
			UnpackDetal param = new UnpackDetal();
			
			//------------jyy2019-09-11-07start----------------
			param.setWarehouse_id(Long.valueOf(warehouse_id));
			//------------jyy2019-09-11-07end------------------
			//------------t 2019-12-11 17 start----------------
			param.setType("0");
			//------------t 2019-12-11 17 end------------------
			List<UnpackDetal> list = UnpackDetalService.queryAllByMution(param );
			
			JSONObject jsonObject = new JSONObject();
			int count = list.size();
			if (count>0) {
				//传递数据到页面
			    jsonObject.put("status", "1");
			    jsonObject.put("message", "");
			    jsonObject.put("data", list);
			}else {
				//传递数据到页面
			    jsonObject.put("status", "0");
			    jsonObject.put("message", "查询数据为空");
			    jsonObject.put("data", list);
			}
			try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		/**
		 * 拆箱
		 * @param json1  所有 拆箱前 的条码信息
		 * @param json2 所有 拆箱后 的条码信息
		 * @param userId 工作量报工情况
		 * @param response  响应         
		 * 
		 * 修改人：吉永艳
		 * 修改时间：2019-11-07
		 * 修改内容：增加仓库id 
		 */
		@RequestMapping("/devanning")
		public void devanning(String json1, String json2, String userId, String unpackDetail, String warehouse_id, HttpServletResponse response) {
			
			JSONObject jsonObject = new JSONObject();
			try {
				int affct = UnpackDetalService.devanning(json1,json2,userId,unpackDetail,warehouse_id);
				
				if (affct > 0) {
					//传递数据到页面
					jsonObject.put("status", "1");
					jsonObject.put("message", "");
					jsonObject.put("data", "提交成功");
				}else if (affct == -1) {
					//传递数据到页面
					jsonObject.put("status", "0");
					jsonObject.put("message", "条码已拆箱或已出库");
					jsonObject.put("data", "0");
				}else if (affct == -2) {
					//传递数据到页面
					jsonObject.put("status", "0");
					jsonObject.put("message", "拆箱明细修改失败，请检查此拆箱单");
					jsonObject.put("data", "0");
				}else if (affct == -3) {
					//传递数据到页面
					jsonObject.put("status", "0");
					jsonObject.put("message", "拆箱单据状态修改失败，请检查拆箱单");
					jsonObject.put("data", "0");
				}else if (affct == -4) {
					//传递数据到页面
					jsonObject.put("status", "0");
					jsonObject.put("message", "此拆箱计划已删除，请核对");
					jsonObject.put("data", "0");
				}else {
					//传递数据到页面
					jsonObject.put("status", "0");
					jsonObject.put("message", "提交失败");
					jsonObject.put("data", "0");
				}
		
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				//传递数据到页面
				jsonObject.put("status", "0");
				jsonObject.put("message", "精准拆箱计划拆箱提交失败");
				jsonObject.put("data", "0");
				logger.error("精准拆箱计划拆箱提交异常");
				try {
					ResponseUtil.write(response, jsonObject);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
		}
}
